package com.example.controller.client;

import com.example.entity.vo.CustomerCardVO;
import com.example.entity.vo.TradeDetailVO;
import com.example.service.CardInfoService;
import com.example.service.CustomerCardService;
import com.example.service.TradeInfoService;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 客户银行卡操作控制器
 */
@Controller
@RequestMapping("/client/card")
public class ClientCardController {

    @Autowired
    private CardInfoService cardInfoService;
    
    @Autowired
    private CustomerCardService customerCardService;
    
    @Autowired
    private TradeInfoService tradeInfoService;

    /**
     * 客户首页
     */
    @GetMapping("/index")
    public String index() {
        return "client/index";
    }
    
    /**
     * 查询余额页面
     */
    @GetMapping("/balance")
    public String balancePage() {
        return "client/balance";
    }
    
    /**
     * 查询余额
     */
    @GetMapping("/query-balance")
    @ResponseBody
    public Result<CustomerCardVO> queryBalance(@RequestParam("cardId") String cardId) {
        return customerCardService.getCardInfo(cardId);
    }
    
    /**
     * 存款页面
     */
    @GetMapping("/deposit")
    public String depositPage() {
        return "client/deposit";
    }
    
    /**
     * 存款操作
     */
    @PostMapping("/do-deposit")
    @ResponseBody
    public Result<?> doDeposit(
            @RequestParam("cardId") String cardId,
            @RequestParam("amount") BigDecimal amount) {
        return cardInfoService.deposit(cardId, amount);
    }
    
    /**
     * 取款页面
     */
    @GetMapping("/withdraw")
    public String withdrawPage() {
        return "client/withdraw";
    }
    
    /**
     * 取款操作
     */
    @PostMapping("/do-withdraw")
    @ResponseBody
    public Result<?> doWithdraw(
            @RequestParam("cardId") String cardId,
            @RequestParam("amount") BigDecimal amount,
            @RequestParam("password") String password) {
        return cardInfoService.withdraw(cardId, amount, password);
    }
    
    /**
     * 转账页面
     */
    @GetMapping("/transfer")
    public String transferPage() {
        return "client/transfer";
    }
    
    /**
     * 转账操作
     */
    @PostMapping("/do-transfer")
    @ResponseBody
    public Result<?> doTransfer(
            @RequestParam("fromCardId") String fromCardId,
            @RequestParam("toCardId") String toCardId,
            @RequestParam("amount") BigDecimal amount,
            @RequestParam("password") String password) {
        return cardInfoService.transfer(fromCardId, toCardId, amount, password);
    }
    
    /**
     * 交易记录查询页面
     */
    @GetMapping("/transactions")
    public String transactionsPage() {
        return "client/transactions";
    }
    
    /**
     * 查询交易记录
     */
    @GetMapping("/query-transactions")
    @ResponseBody
    public Result<List<TradeDetailVO>> queryTransactions(@RequestParam("cardId") String cardId) {
        return tradeInfoService.getTradesByCardId(cardId);
    }
    
    /**
     * 挂失页面
     */
    @GetMapping("/report-loss")
    public String reportLossPage() {
        return "client/report-loss";
    }
    
    /**
     * 挂失操作
     */
    @PostMapping("/do-report-loss")
    @ResponseBody
    public Result<?> doReportLoss(@RequestParam("cardId") String cardId) {
        return cardInfoService.reportLoss(cardId);
    }
    
    /**
     * 解除挂失操作
     */
    @PostMapping("/do-cancel-loss")
    @ResponseBody
    public Result<?> doCancelLoss(@RequestParam("cardId") String cardId) {
        return cardInfoService.cancelLoss(cardId);
    }
    
    /**
     * 修改密码页面
     */
    @GetMapping("/change-password")
    public String changePasswordPage() {
        return "client/change-password";
    }
    
    /**
     * 修改密码操作
     */
    @PostMapping("/do-change-password")
    @ResponseBody
    public Result<?> doChangePassword(
            @RequestParam("cardId") String cardId,
            @RequestParam("oldPassword") String oldPassword,
            @RequestParam("newPassword") String newPassword) {
        return cardInfoService.changePassword(cardId, oldPassword, newPassword);
    }
} 